package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.genesis.yunnanji.Constants;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.TrolleyListBean;

import java.util.List;


public class OrderAdapter extends RecyclerView.Adapter {

    Context context;

    private List<TrolleyListBean.ListBean> mDates;

    private OnItemClickListener mOnItemClickListener;

    private ModifyCountInterface modifyCountInterface;

    public OrderAdapter(Context context, List<TrolleyListBean.ListBean> date) {
        this.context = context;
        this.mDates = date;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int pos, List<TrolleyListBean.ListBean> myLiveList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shopping_trolley, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return mDates.size();
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final OrderViewHolder viewHolder = (OrderViewHolder) holder;
        final TrolleyListBean.ListBean bean = mDates.get(holder.getAdapterPosition());

        viewHolder.tvTitle.setText(mDates.get(position).getProduct_name());
        viewHolder.tvStyle.setText(mDates.get(position).getColor_name());
        viewHolder.tvSpecification.setText(mDates.get(position).getSize_name());
        viewHolder.tvPrice.setText("¥" + mDates.get(position).getSale_price());
        viewHolder.tvMun.setText(mDates.get(position).getNums());
        Glide.with(context).load(Constants.PIC_ADMIN + mDates.get(position).getCover_img()).into(viewHolder.ivGoods);

        if (bean.isSelect()) {
            viewHolder.checkBox.setImageResource(R.drawable.icon_choose);
        } else {
            viewHolder.checkBox.setImageResource(R.drawable.icon_unchoose);
        }
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(holder.getAdapterPosition(), mDates);
            }
        });

        viewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doIncrease(position, viewHolder.tvMun, viewHolder.checkBox.isSelected());
            }
        });
        viewHolder.tvSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position, viewHolder.tvMun, viewHolder.checkBox.isSelected());
            }
        });

    }

    /**
     * 购物车商品集合
     */
    class OrderViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivGoods;
        public TextView tvTitle;
        public TextView tvStyle;
        public TextView tvSpecification;
        public TextView tvPrice;
        public ImageView checkBox;
        public TextView tvAdd;
        public TextView tvSub;
        public TextView tvMun;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            ivGoods = (ImageView) itemView.findViewById(R.id.iv_goods);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvStyle = (TextView) itemView.findViewById(R.id.tv_style);
            tvSpecification = (TextView) itemView.findViewById(R.id.tv_specification);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            checkBox = (ImageView) itemView.findViewById(R.id.check_box);
            tvMun = (TextView) itemView.findViewById(R.id.tv_commodity_show_num);
            tvSub = (TextView) itemView.findViewById(R.id.tv_sub);
            tvAdd = (TextView) itemView.findViewById(R.id.tv_add);
        }
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }

}
